package org.dubbo.demo.consumer;

import com.dubbo.demo.sdk.api.GreetingService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcContext;

/**
 * @author: Carlos
 * @create: 2021/02/08 10:45
 */
public class ApiConsumer {

    public static void main(String[] args) throws InterruptedException {
        // 1.创建服务引用对象实例
        ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>();

        // 2.设置应用程序信息
        referenceConfig.setApplication(new ApplicationConfig("second-dubbo-provider"));

        // 3.设置服务注册中心
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://47.98.50.37:2181"));

        // 4.设置服务接口和超时时间
        referenceConfig.setInterface(GreetingService.class);
        referenceConfig.setTimeout(5000);

        // 5.设置服务分组和版本
        referenceConfig.setVersion("1.0.0");
        referenceConfig.setGroup("dubbo");

        // 引用服务
        GreetingService greetingService = referenceConfig.get();

        RpcContext.getContext().setAttachment("company", "alibaba");

        System.out.println(greetingService.sayHello("World"));

        Thread.currentThread().join();
    }

}
